# 一切皆对象
"""
class Foo:
    def find(self):
        print('我是绑定对象的方法')
print(Foo) # <class '__main__.Foo'>
obj = Foo()
print(obj) # <__main__.Foo object at 0x0214ECF0>
obj.find()
print(type(obj))

print(str)
name = str('longzai')
print(name)

# print(str,list,tuple,set,dict,int) # 都有对应的类去实现
"""
# pymysql连接
"""
mysql可以怎么连接它？
可以在xshell里面，输入mysql账号、密码去使用
可以在远程登录工具中：sqlyag去使用
可以通过python连接mysql
"""
# import pymysql
# HOST = "121.196.200.187"
# USER = 'root'
# PWD = '123456'
# DATABASE = 'weiyanxu'
#
# # 创建pymysql 对象
# pymysql_con = pymysql.connect(host = HOST,user = USER,password=PWD
#                 ,database=DATABASE,cursorclass=pymysql.cursors.DictCursor)
# 创建游标
"""
游标就是可以用它到数据库取值，提交对数据库的改动
"""
"""
cursor = pymysql_con.cursor() # 游标对象

# 创建数据表
# cursor.execute('create table student(id int primary key auto_increment,name varchar(20))')
# pymysql_con.commit()
# 查询数据库内容
result = cursor.execute('show tables;')
print(result)
# 查看数据表结构
result = cursor.execute('desc student')
print(result)
# 增加数据
cursor.execute('insert into student(name) values("asd")')
pymysql_con.commit()
# 查询数据
# cursor.execute('select * from student')
# result = cursor.fetchone() # 返回符合sql条件的一条数据
# print(result)
# 查询数据全部
# cursor.execute('select * from student')
# result = cursor.fetchall()
# for i in result:
#     print(i)
#修改数据
cursor.execute('update student set name="ppp" where id = 1')
pymysql_con.commit()

cursor.execute('select * from student')
result = cursor.fetchall()
print(result)
cursor.close() # 关闭游标
pymysql_con.close() # 关闭数据库
"""
# 使用类封装数据库

#import pymysql
# pip install pymysql
# class DB:
#     def __init__(self,host,user,password,database,cursorclass = None):
#         """
#         :param host: 就是阿里云服务器的ip地址
#         :param user: 数据库的账号名  一般都是root
#         :param password: 数据库密码
#         :param database: 数据库的名字
#         :param cursorclass:  返回的格式是字典格式，有key和value
#         """
#         self.host = host
#         self.user = user
#         self.password = password
#         self.databases = database
#         self.cursorclass = cursorclass
#     def connect(self):
#         self.db = pymysql.connect(host = self.host,user = self.user,
#                                   password = self.password,database = self.databases,cursorclass=self.cursorclass)
#         self.cursor = self.db.cursor() # todo：创建游标， 对数据库有真实的增删改的时候，需要提交数据库和游标
#         # 游标，对数据库有增、删、改的时候，需要提交游标还有数据库对象，不然无效
#         # 查询的时候不需要提交游标
#     def close(self):
#         self.cursor.close() # 关闭游标
#         self.db.close() # 关闭数据库
#     def select_one(self,sql): # select * from student where id = 3;   10
#         self.connect() # 第一步，调用连接数据库方法，连接数据库
#         try: # 尝试去执行sql
#             self.cursor.execute(sql) # 使用游标对象，执行sql语句
#             result = self.cursor.fetchone() # 返回一条数据 fetchone()
#             self.close() # 最后关闭掉数据库，不要忘记
#         except Exception as e:  # 如果sql执行错误
#             print("select error %s"%e)
#             result = None
#         return result
#     def select_all(self,sql): #  select * from student where id = 3; 10
#         self.connect()
#         try:
#             self.cursor.execute(sql)
#             result = self.cursor.fetchall()  # 返回一条数据
#             self.close()
#         except Exception as e:
#             print("select error %s" % e)
#             result = None
#         return result
#     def __edit(self,sql): # 相当于是一个主程序
#         self.connect() # 先连接数据库
#         try:
#             self.cursor.execute(sql) # 使用游标对象，执行sql语句
#             self.db.commit() # 数据库对象提交
#             result = "数据库更新成功"
#         except Exception as e:
#             result = "数据库更新失败"%e
#         return result
#     def insert(self,sql): # insert into student(3,11,55);
#         return self.__edit(sql)
#     def delete(self,sql): # delete from student where id = 3;
#         return self.__edit(sql)
#     def update(self,sql): # update student set id = 3 where id =5;
#         return self.__edit(sql)
#
# db1 = DB(host = "47.104.65.165",user="root",password="123456",database = "test",cursorclass =
# pymysql.cursors.DictCursor)
#
#
# print(db1.select_one("select * from student where name = '小天才'"))
# # db1.insert("insert into student(name,age) values('小天才',98)")
# print(db1.select_one("select * from student where name = '小天才'"))
#
# # db1.update("update student set name='大天才' where name = '小天才'")
# print(db1.delete("delete from student where id=1"))
# print(db1.select_all(f"select * from {'student'}"))










